//
//  CSGXTimeBar.m
//  CarSalesMS_GCX
//
//  Created by Ssky on 2016/11/16.
//  Copyright © 2016年 Tony. All rights reserved.
//

#import "CSGXTimeBar.h"
#import "CSTextField.h"
#import "CSGXDatePicker.h"

@interface CSGXTimeBar () <UITextFieldDelegate, CSGXDatePickerDelegate>

@property (nonatomic,weak) CSTextField * txtTimeBegin;
@property (nonatomic,weak) UILabel * lblLine;
@property (nonatomic,weak) CSTextField * txtTimeEnd;


@property (nonatomic, weak) CSTextField *currentText;

@property (nonatomic, weak) CSGXDatePicker *datePicker;

@end

@implementation CSGXTimeBar

- (void)layoutSubviews {
    
    [self callBackTimeArr];
}

#pragma mark - methods

- (void)callBackTimeArr {
    
    NSString * tBeginStr = self.txtTimeBegin.text;
    NSString * tEndStr = self.txtTimeEnd.text;
    
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    [fmt setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_Hans_CN"]];
    fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    
    NSString *beginStr = [NSString stringWithFormat:@"%@ 00:00:00", tBeginStr];
    NSString *endStr = [NSString stringWithFormat:@"%@ 23:59:59", tEndStr];
    
    NSDate * dateBegin = [fmt dateFromString:beginStr];
    NSDate * dateEnd = [fmt dateFromString:endStr];
    
    NSLog(@"beginStr: %@", beginStr);
    NSLog(@"endStr: %@", endStr);
    NSLog(@"dateBegin: %@", dateBegin);
    NSLog(@"dateEnd: %@", dateEnd);
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
    });
    
    // 判断合法性
    if (dateBegin > dateEnd) {
        NSLog("开始时间不能大于结束时间！");
        
        //call Back
        if (self.callBackText) {
            self.callBackText(NO, @[@"", @""]);
        }
        
        return;
    }
    
    // call Back
    if (self.callBackText) {
        self.callBackText(YES, @[tBeginStr, tEndStr]);
    }
}

#pragma mark - actions
- (void)textAction:(NSNotification *)note {
    //    NSLog(@"textAction --note.object: %@", note.object);
    
    [self callBackTimeArr];
}


#pragma mark - delegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    self.currentText = (CSTextField *)textField;
    //alert DatePicker
    CSGXDatePicker *datePicker = [[CSGXDatePicker alloc] init];
    [[UIApplication sharedApplication].keyWindow addSubview:datePicker];
    self.datePicker = datePicker;
    
    [datePicker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.superview);
        make.size.mas_offset(datePicker.bounds.size);
    }];
    self.datePicker.delegate = self;
    datePicker.pickerType = PickerTypeDate;

    return NO;
}


- (void)gxDataPicker:(CSGXDatePicker *)datePicker time:(NSString *)time {
    NSLog(@"gxDataPicker-time: %@", time);
    
    self.currentText.text = time;
    [self textAction:nil];
}


#pragma markl - setupUI
- (void)setupUI {
    //txtTimeBegin
    CSTextField * txtTimeBegin = [CSTextField gx_textFieldWithLineColor:[UIColor grayColor] lineWidth:2];
    [self addSubview:txtTimeBegin];
    txtTimeBegin.textAlignment = NSTextAlignmentCenter;
    txtTimeBegin.font = [UIFont systemFontOfSize:12];
    txtTimeBegin.selectedLineColor = [UIColor grayColor];
    txtTimeBegin.text = [[CSGXDateFormatterManager sharedManager] currentDay]; //@"2016-12-10"; //test
    txtTimeBegin.delegate = self;
    
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textAction:) name:UITextFieldTextDidChangeNotification object:txtTimeBegin];
    
    [txtTimeBegin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.top.leading.equalTo(self).offset(3);
        make.size.mas_offset(CGSizeMake(90, 20));
    }];
    
    //lblLine
    UILabel * lblLine = [UILabel gx_labelWithText:@"-" fontSize:13 color:nil];
    [self addSubview:lblLine];
    
    [lblLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.top.equalTo(txtTimeBegin);
        make.leading.equalTo(txtTimeBegin.mas_trailing).offset(4);
    }];
    
    //txtTimeEnd
    CSTextField * txtTimeEnd = [CSTextField gx_textFieldWithLineColor:[UIColor grayColor] lineWidth:2];
    [self addSubview:txtTimeEnd];
    txtTimeEnd.textAlignment = NSTextAlignmentCenter;
    txtTimeEnd.font = [UIFont systemFontOfSize:12];
    txtTimeEnd.selectedLineColor = [UIColor grayColor];
    txtTimeEnd.text = [[CSGXDateFormatterManager sharedManager] currentDay];
    txtTimeEnd.delegate = self;
    
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textAction:) name:UITextFieldTextDidChangeNotification object:txtTimeEnd];
    
    [txtTimeEnd mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.centerY.top.equalTo(txtTimeBegin);
        make.leading.equalTo(lblLine.mas_trailing).offset(4);
    }];
    
    //
    self.txtTimeBegin = txtTimeBegin;
    self.txtTimeEnd = txtTimeEnd;
    self.lblLine = lblLine;
    
}

@end
